
// Assist setup for new app installations
// original provided by gemi254
// 
// s60sc 2023

#include "appGlobals.h"

#if (!INCLUDE_CERTS)
const char* git_rootCACertificate = "";
#endif

static fs::FS fp = STORAGE;

static bool wgetFile(const char* filePath) {
  // download required data file from github repository and store
  bool res = false;
  if (fp.exists(filePath)) {
    // if file exists but is empty, delete it to allow re-download
    File f = fp.open(filePath, FILE_READ);
    size_t fSize = f.size();
    f.close();
    if (!fSize) fp.remove(filePath);
  }
  if (!fp.exists(filePath)) {
    char downloadURL[150];
    snprintf(downloadURL, 150, "%s%s", GITHUB_PATH, filePath);
    File f = fp.open(filePath, FILE_WRITE);
    if (f) {
      NetworkClientSecure wclient;
      if (remoteServerConnect(wclient, GITHUB_HOST, HTTPS_PORT, git_rootCACertificate, SETASSIST)) {
        HTTPClient https;
        if (https.begin(wclient, GITHUB_HOST, HTTPS_PORT, downloadURL, true)) {
          LOG_INF("Downloading %s from %s", filePath, downloadURL);    
          int httpCode = https.GET();
          int fileSize = 0;
          if (httpCode == HTTP_CODE_OK) {
            fileSize = https.writeToStream(&f);
            if (fileSize <= 0) {
              LOG_WRN("Download failed: writeToStream - %s", https.errorToString(fileSize).c_str());
              httpCode = 0;
            } else LOG_INF("Downloaded %s, size %s", filePath, fmtSize(fileSize));       
          } else LOG_WRN("Download failed, error: %s", https.errorToString(httpCode).c_str());    
          https.end();
          f.close();
          if (httpCode == HTTP_CODE_OK) {
            if (!strcmp(filePath, CONFIG_FILE_PATH)) doRestart("Config file downloaded");
            res = true;
          } else {
            LOG_WRN("HTTP Get failed with code: %d", httpCode);
            fp.remove(filePath);
          }
        }
      } 
      remoteServerClose(wclient);
    } else LOG_WRN("Open failed: %s", filePath);
  } else res = true;
  return res;
}

bool checkDataFiles() {
  // Download any missing data files
  bool res = false;
  if (strlen(GITHUB_PATH)) {
    res = wgetFile(COMMON_JS_PATH); 
    if (res) res = wgetFile(INDEX_PAGE_PATH); 
    if (res) res = appDataFiles(); 
  } else res = true; // no download needed
  return res;
}

// To customise the the setupPage, use example in extras folder setupPage.html and then use cyberchef.org to convert to hex (see example in extras folder for cyberchef settings)
const uint8_t setupPage_html_gz[] PROGMEM = {
  0x1f, 0x8b, 0x08, 0x08, 0xbd, 0xf2, 0x0a, 0x67, 0x00, 0xff, 0x73, 0x65, 0x74, 0x75, 0x70, 0x50, 
  0x61, 0x67, 0x65, 0x2e, 0x68, 0x74, 0x6d, 0x6c, 0x00, 0xc5, 0x38, 0xeb, 0x72, 0xdb, 0xba, 0xd1, 
  0xff, 0xfd, 0x14, 0x3c, 0x3a, 0xe9, 0x50, 0xea, 0xb1, 0x28, 0xc9, 0x8e, 0x1d, 0x47, 0x14, 0x95, 
  0x91, 0x64, 0x27, 0x71, 0xe2, 0xdc, 0x2c, 0xdb, 0xc9, 0x49, 0xa7, 0xd3, 0x40, 0x24, 0x48, 0xc2, 
  0x86, 0x00, 0x06, 0x80, 0x6e, 0xf6, 0xe8, 0x25, 0xda, 0xfe, 0xff, 0x5e, 0xb1, 0x8f, 0x70, 0x16, 
  0x20, 0x29, 0x51, 0x17, 0xab, 0x49, 0xa7, 0xdf, 0x54, 0x9a, 0x21, 0x09, 0x60, 0x77, 0xb1, 0xd7, 
  0x0b, 0xd0, 0xfa, 0x25, 0xe0, 0xbe, 0x9a, 0x25, 0xd8, 0x8a, 0xd5, 0x90, 0xb6, 0xf7, 0x5a, 0xf9, 
  0x0b, 0xa3, 0x00, 0x5e, 0x43, 0xac, 0x90, 0xe5, 0xc7, 0x48, 0x48, 0xac, 0xbc, 0x91, 0x0a, 0xab, 
  0x27, 0xf9, 0x24, 0x43, 0x43, 0xec, 0x8d, 0x09, 0x9e, 0x24, 0x5c, 0x28, 0xcb, 0xe7, 0x4c, 0x61, 
  0xa6, 0xbc, 0xd2, 0x84, 0x04, 0x2a, 0xf6, 0x02, 0x3c, 0x26, 0x3e, 0xae, 0x9a, 0xc1, 0x3e, 0x61, 
  0x44, 0x11, 0x44, 0xab, 0xd2, 0x47, 0x14, 0x7b, 0x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63, 0x2c, 
  0x42, 0x0a, 0xf0, 0x31, 0x09, 0x02, 0xcc, 0xdc, 0x49, 0x4c, 0x14, 0xae, 0xca, 0x04, 0xf9, 0x18, 
  0xb8, 0x98, 0x08, 0x94, 0x6c, 0xb2, 0x69, 0x05, 0x64, 0xbc, 0xbf, 0x39, 0x3b, 0x18, 0x29, 0xc5, 
  0xd9, 0x43, 0xca, 0xd9, 0x80, 0x02, 0xaf, 0x6e, 0x82, 0x82, 0x80, 0xb0, 0xa8, 0xd9, 0x38, 0xc8, 
  0x78, 0x71, 0x15, 0x9e, 0xaa, 0x6a, 0x80, 0x7d, 0x2e, 0x8c, 0x55, 0x52, 0x49, 0x73, 0xb1, 0x43, 
  0x8a, 0xa7, 0xee, 0xed, 0x48, 0x2a, 0x12, 0xce, 0x72, 0xb2, 0x4d, 0xc3, 0x4b, 0x75, 0x80, 0xd5, 
  0x04, 0x03, 0x7f, 0x88, 0x92, 0x88, 0x55, 0x81, 0xc7, 0xa1, 0x6c, 0xfa, 0xb0, 0x8c, 0xc5, 0xaa, 
  0xfc, 0x22, 0xc0, 0xa2, 0x59, 0x2f, 0x68, 0x0a, 0x06, 0xfe, 0x48, 0x48, 0x60, 0x29, 0xe1, 0x44, 
  0xc3, 0x6f, 0x97, 0xa7, 0x19, 0x6b, 0x4d, 0x3c, 0x2a, 0x55, 0xba, 0xbc, 0xc5, 0x53, 0xc2, 0x86, 
  0xfe, 0xcf, 0x1d, 0x06, 0xfc, 0x71, 0x71, 0x07, 0xa2, 0x29, 0x44, 0xa8, 0x5c, 0x58, 0xd2, 0x88, 
  0xb4, 0x85, 0xe9, 0x08, 0x25, 0x4d, 0x30, 0xd0, 0xdc, 0x91, 0xb0, 0x04, 0xae, 0x4b, 0x60, 0xbf, 
  0x87, 0x4c, 0x12, 0xad, 0xa7, 0x18, 0x93, 0x28, 0x56, 0xe9, 0x77, 0x61, 0x57, 0x32, 0x44, 0x11, 
  0x6e, 0x8e, 0x04, 0x2d, 0xdb, 0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0x75, 0x14, 0x9c, 0x66, 0x0d, 0x17, 0x32, 0x18, 0xc3, 0xe2, 0xf5, 0xd5, 0xbb, 0x0b, 0xcf, 0xb6, 
  0x5d, 0x1d, 0x07, 0x94, 0xb3, 0x08, 0x4b, 0xa5, 0xc5, 0xd2, 0x53, 0xb9, 0x69, 0x9d, 0x90, 0x8b, 
  0x33, 0x04, 0x8a, 0xcc, 0xac, 0x0b, 0xaa, 0x04, 0x99, 0xb3, 0x81, 0xa3, 0xa1, 0x1c, 0x8a, 0x59, 
  0xa4, 0xe2, 0x76, 0x81, 0x40, 0x36, 0x55, 0x79, 0x28, 0x12, 0x2d, 0xe2, 0x18, 0x81, 0x53, 0xa1, 
  0xa0, 0x2e, 0x27, 0x19, 0xf3, 0x4b, 0x81, 0x7c, 0x81, 0x41, 0x01, 0xd9, 0x34, 0xf8, 0x7f, 0x02, 
  0xea, 0xab, 0x40, 0x7a, 0x59, 0x80, 0xa6, 0x09, 0xc0, 0x19, 0x13, 0x49, 0x06, 0x84, 0x12, 0x35, 
  0xf3, 0xec, 0xb4, 0x87, 0xb2, 0xb7, 0x40, 0x99, 0x4c, 0xd6, 0xd7, 0x9d, 0x8c, 0x67, 0xa7, 0x6d, 
  0xd5, 0x2a, 0x54, 0xd1, 0x54, 0x05, 0x8e, 0xdd, 0x05, 0x3b, 0xba, 0x51, 0x75, 0x50, 0x92, 0x60, 
  0x16, 0xf4, 0x62, 0x42, 0x83, 0x72, 0x01, 0x3b, 0x97, 0x23, 0x57, 0xf0, 0x67, 0xd3, 0x21, 0x17, 
  0xc9, 0xf3, 0x30, 0x84, 0xf6, 0xd7, 0xcc, 0xff, 0x06, 0x75, 0x62, 0x8d, 0xac, 0xc0, 0x43, 0xc8, 
  0x5e, 0x5b, 0xc8, 0xae, 0x5b, 0x2c, 0x93, 0xc5, 0x90, 0xff, 0xf6, 0xe4, 0x61, 0x65, 0xbf, 0x79, 
  0x32, 0xfd, 0xb6, 0xcb, 0x64, 0x26, 0xd1, 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, 0x79, 0x8e, 0x2a, 0xb5, 0xe1, 0x8b, 0xc1, 
  0x59, 0xa4, 0x55, 0x4b, 0x21, 0x7f, 0x86, 0x64, 0x6a, 0xd8, 0x2c, 0x5c, 0x20, 0x69, 0x9b, 0xb3, 
  0x3a, 0xf4, 0xa1, 0x3a, 0x6e, 0xca, 0x95, 0x76, 0x0f, 0x31, 0x1f, 0xd3, 0x02, 0xe1, 0xa2, 0x60, 
  0x35, 0xe3, 0xfc, 0x26, 0x24, 0xac, 0x5a, 0x3b, 0x7b, 0x22, 0x2b, 0x16, 0x38, 0xf4, 0x6a, 0x13, 
  0x3c, 0x78, 0xf1, 0xe1, 0xaa, 0xe3, 0xc0, 0x1d, 0x33, 0x2c, 0x3d, 0xca, 0x42, 0x1b, 0x60, 0xf6, 
  0xae, 0xcd, 0x11, 0x67, 0xb1, 0x4b, 0xab, 0x86, 0x34, 0xf5, 0x85, 0x7e, 0x6a, 0x59, 0xe4, 0xd5, 
  0xcc, 0x7d, 0xf5, 0x1f, 0xb5, 0xcc, 0x6b, 0x95, 0xc6, 0x16, 0x00, 0x00
  };

const size_t setupPage_html_gz_len = sizeof(setupPage_html_gz);

const char* setupPage_html = R"~(
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <title>Application setup</title> 
</head>
<script>
function Config(){
  if (!window.confirm('This will reboot the device to activate new settings.'))  return false; 
  fetch('/control?ST_SSID=' + encodeURI(document.getElementById('ST_SSID').value))
  .then(r => { console.log(r); return fetch('/control?ST_Pass=' + encodeURI(document.getElementById('ST_Pass').value)) })
  .then(r => { console.log(r); return fetch('/control?save=1') })     
  .then(r => { console.log(r); return fetch('/control?reset=1') })
  .then(r => { console.log(r); }); 
  return false;
}
</script>
<body style="font-size:18px">
<br>
<center>
  <table border="0">
    <tr><th colspan="3">Wifi setup..</th></tr>
    <tr><td colspan="3"></td></tr>
    <tr>
    <td>SSID</td>
    <td>&nbsp;</td>
    <td><input id="ST_SSID" name="ST_SSID" length=32 placeholder="Router SSID" class="input"></td>
  </tr>
    <tr>
    <td>Password</td>
    <td>&nbsp;</td>
    <td><input id="ST_Pass" name="ST_Pass" length=64 placeholder="Router password" class="input"></td>
  </tr>
  <tr><td colspan="3"></td></tr>
    <tr><td colspan="3" align="center">
        <button type="button" onClick="return Config()">Connect</button>&nbsp;<button type="button" onclick="window.location.reload;">Cancel</button>
    </td></tr>
  </table>
  <br/><br/><a href="/web?OTA.htm"><button>OTA Update</button></a>
</center>      
</body>
</html>
)~";

const char* otaPage_html = R"~(
<!DOCTYPE html>
<html>
  <head>
    <title>OTA</title>
    <style>
      html body {height: 100%;}
      body {
        font-family: Helvetica  !important;
        background: #181818;
        color: WhiteSmoke;
        font-size: 1rem;; 
      }
    </style>
  </head>
  <body>
    <br>
    <h3>Upload data file or bin file to ESP32</h3>
    <br>
    <a href="javascript:history.back()" style="color: WhiteSmoke;">Go Back</a>
    <br><br><br>
    <form id="upload_form" enctype="multipart/form-data" method="post">
      <input type="file" name="otafile" id="otafile" onchange="otaUploadFile()"><br>
      <br>
      <progress id="progressOta" value="0" max="100" style="width:200px;"></progress>%
      <h3 id="status"></h3>
      <p id="loaded_n_total"></p>
    </form>
    
    <script>
      const defaultPort = window.location.protocol == 'http:' ? 80 : 443; 
      const webPort = !window.location.port ? defaultPort : window.location.port; // in case alternative ports specified
      const webServer = window.location.protocol + '//' + document.location.hostname + ':' + webPort;
      const $ = document.querySelector.bind(document);
   
      async function otaUploadFile() {
        // notify server to start ota 
        let file = $("#otafile").files[0];
        const response = await fetch('/control?startOTA=' + file.name);
        if (response.ok) {
          // submit file for uploading
          let xhr = new XMLHttpRequest();
          xhr.upload.addEventListener("progress", progressHandler, false);
          xhr.addEventListener("load", completeHandler, false);
          xhr.addEventListener("error", errorHandler, false);
          xhr.addEventListener("abort", abortHandler, false);
          xhr.open("POST", webServer +  '/upload');
          xhr.send(file);
        } else alert(response.status + ": " + response.statusText); 
      }

      function progressHandler(event) {
        $("#loaded_n_total").innerHTML = "Uploaded " + event.loaded + " of " + event.total + " bytes";
        let percent = (event.loaded / event.total) * 100;
        $("#progressOta").value = Math.round(percent);
        $("#status").innerHTML = Math.round(percent) + "% transferred";
        if (event.loaded  == event.total) $("#status").innerHTML = 'Uploaded, wait for completion result';
      }

      function completeHandler(event) {
        $("#status").innerHTML = event.target.responseText;
        $("#progressOta").value = 0;
      }

      function errorHandler(event) {
        $("#status").innerHTML = "Upload Failed";
        $("#progressOta").value = 0;
      }

      function abortHandler(event) {
        $("#status").innerHTML = "Upload Aborted";
        $("#progressOta").value = 0;
      }
    </script>
  </body>
</html>
)~";

const char* failPageS_html = R"~(
<!DOCTYPE html>
<html>
  <head>
    <title>Startup Failure</title>
    <script>
      function getLog() {
        fetch('/control?displayLog=1')
        .then(response => response.text())
        .then(logdata => { document.getElementById('appLog').innerText = logdata;})
        .catch(error => alert('Error fetching log:', error));
      }
    </script>
  </head>
  <body>
    <h2>
)~";

const char* failPageE_html = R"~(
    </h2>
    <h3><a href="#" onclick="getLog(); return false;">Check log</a></h3>
    <h3><a href='/control?reset=1' class='button'>Reboot ESP after fix</a></h3>
    <div id="appLog"></div>
  </body>
</html>
)~";
